<!--chatRoom不共用，当前只用于竞赛演练场景聊天,用于获取用户及群组数据-->
<!--必须传递typeId参数-->
<!--目前只有私聊-->
<!--如果有群聊(添加传参,groupList,groupType[0.工程接入组 1.课件用户组 2.自定义群组],getGroupList:查询组接口)-->
<template>
    <chat
          ref="chatRoom" 
          type="drill" :isDistributed="isDistributed"
          :typeId="drillId"
          :userList="userList"
		  :userListAll="userListbf"
          @filterUsers="getDrillUsers"></chat>
</template>

<script>
  import {getuserinstance} from '@/api/access_new.js'
//import encryption from '@/libs/encryption.js'
  import chat from "./components/chat"
  import {mapState} from 'vuex'
  export default {
    name: 'chatRoom',
    props:['drillId','isDistributed'],
    components:{chat},
    data(){
//    let October = sessionStorage.getItem('November')
//    October = JSON.parse(encryption.SelfUseDecrypt(October))
      return {
//    	USER_ID:October.userId,
//      USER_Name:October.nickname,
//      avatar:October.avatar,
		userList:[],
		userListbf:[],
		queryDrillUserInfo:{
      		pageIndex:1,
     	 	pageSize:2147483646, //查询所有
     	 	testId:'',
      	},
      	totalCount:0,
      	showChat:false
      }
    },
    filters:{
    	
    },
    computed:{   
    	...mapState('board',['clearNotice']), //新消息
		...mapState('compet',['refreshChatRoom']) // 数据变化，需要刷新列表
    },
    watch:{
    	clearNotice: {
			handler(val, oldval) {
				if(val.sceneInfo.id == this.drillId){
					this.clearChatMsg();
				}
			},
			deep: true
		},
		refreshChatRoom() {
			this.refreshData()
		}
    },
    mounted(){
    	this.queryDrillUserInfo.testId = this.drillId;
        this.getDrillUsers(1);
    },
    beforeDestroy(){
    	
    },
    methods:{
		// 更新数据
		refreshData() {
			this.getDrillUsers(1, '', true);
			this.$refs.chatRoom.content = '' // 清空聊天内容
		},
	    //获取场景用户
        getDrillUsers(page, userName, isRefresh){
			if (!isRefresh) { // 非更新操作，操作原始数据
				if(this.userListbf.length > 0 && (this.userListbf.length == this.totalCount)){
					if(userName){
						this.userList = this.userListbf.filter(user => user.userName.indexOf(userName)!=-1);
					}else{
						this.userList = this.userListbf
					}
					return
				}
			}
        	if(page == 1){
        		this.userList = [];
        		this.userListbf = [];
        		this.queryDrillUserInfo.pageIndex  = page;
        	}else{
        		this.queryDrillUserInfo.pageIndex ++; 
        	}
        	if(userName){
        		this.queryDrillUserInfo.nickname = userName;
        	}else{
        		this.queryDrillUserInfo.nickname = '';
        	}
	     	getuserinstance(this.queryDrillUserInfo).then(data => {
	     	 	if(data.code == 1){
	     	 		 this.totalCount = data.data.total;
	     	 		 this.userListbf = this.userListbf.concat(data.data.items);
	     	 		 this.userList = this.userList.concat(data.data.items);
	     	 		 this.userList.forEach((user) => {
	     	 		 	 this.$set(user,'msgList',[]); //当前显示消息
	     	 		 	 this.$set(user,'msgCount',0); //当前未读消息数
	     	 		 });
	     	 	}else{
	     	 		
	     	 	}
	     	})
	    },
	    updateUser(){
	    	this.getDrillUsers(1);
	    },
	    clearChatMsg(){
	    	this.$refs['chatRoom'].clearMsg();
	    }
    },
    destroyed() {
      
    }
  }
</script>

<style>
	
</style>
